package com.start.security.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.start.common.api.Result;
import com.start.security.annotations.IgnoreAuth;
import com.start.security.annotations.StartUrlDataSecurity;
import com.start.security.model.ClientVerificationModel;
import com.start.security.model.StartDataSecurityModel;
import com.start.security.service.StartSecurityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "客户端密钥管理")
@RestController
@RequestMapping("/start/data/security")
public class StartDataSecurityController {
    @Autowired
    private StartSecurityService startSecurityService;

    @IgnoreAuth
    @StartUrlDataSecurity(value = false)
    @PostMapping("/getClientDecryptionKey")
    @Operation(summary = "创建客户端端密钥")
    public Result<?> getClientDecryptionKey(@RequestBody ClientVerificationModel clientKeyModel) {
        StartDataSecurityModel clientPrivateKey = startSecurityService.createClientSymmetricKey(clientKeyModel.getData());
        return Result.ok(clientPrivateKey);
    }

    @Operation(summary = "刷新客户端端密钥")
    @GetMapping("/updateClientDecryptionKey/{clientId}")
    public Result<?> updateClientDecryptionKey(@PathVariable("clientId") String clientId) {
        startSecurityService.updateClientDecryptionKey(clientId);
        return Result.ok();
    }

    @Operation(summary = "删除客户端端密钥")
    @GetMapping("/deleteClientDecryptionKey/{clientId}")
    public Result<?> deleteClientDecryptionKey(@PathVariable("clientId") String clientId) {
        startSecurityService.deleteClientDecryptionKey(clientId);
        return Result.ok();
    }

    @Operation(summary = "删除客户端端密钥")
    @GetMapping("/deleteCurrentClientDecryptionKey")
    public Result<?> deleteCurrentClientDecryptionKey(HttpServerRequest request) {
        startSecurityService.deleteCurrentClientDecryptionKey(request);
        return Result.ok();
    }
}
